//
// Created by shine on 2019/11/21.
//

#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/opencv.hpp>
#include "MakeFileDir.h"
using namespace std;
using namespace cv;

struct SamplePointWithWeight{
    double yaw;
    double pitch;
    double weight;
};

int main(int argc, char* argv[]){

    string videoName = argv[1];
    cout << "videoName:" << videoName << endl;

    createDirectory("VR2020RawData/samplePointsFiles/" + videoName + "/of/");
    createDirectory("VR2020RawData/samplePointsFiles/" + videoName + "/sm/");
    createDirectory("VR2020RawData/samplePointsFiles/" + videoName + "/roi/");
    createDirectory("VR2020RawData/samplePointsFiles/" + videoName + "/pro/");

    for(int index = 0; index < 30; index ++ ){

///sm
        string inputPrefix = "VR2020RawData/saliencyMap/H/" + videoName+"/";
        string outputPrefix = "VR2020RawData/samplePointsFiles/" + videoName + "/sm/";
//        string ImgPath = inputPrefix + videoName +"_1920x960_30f_8b_420_"+ to_string(index) +"_sal_head.jpg";///Aircraft
        string ImgPath = inputPrefix + videoName +"_1920x960_"+ to_string(index) +"_sal_head.jpg";
        ofstream outFile(outputPrefix + "samplePoints_sm_" + to_string(index) + ".txt");

///roi
//        string inputPrefix = "VR2020RawData/ROI/" + videoName + "/";
//        string outputPrefix = "VR2020RawData/samplePointsFiles/" + videoName + "/roi/";
//        string ImgPath = inputPrefix + videoName + "_" + to_string(index) + ".png";
//        ofstream outFile(outputPrefix + "samplePoints_roi_" + to_string(index) + ".txt");

        Mat srcImg = imread(ImgPath,IMREAD_GRAYSCALE);
        Mat img;

        if (srcImg.empty())  // 判断读入的图片是否为空
        {
            printf("Could not load image...\n");
            system("pause");
            return -1;
        }

        resize(srcImg, img, Size(1920, 960));

    //    namedWindow("Img", CV_WINDOW_NORMAL);

        vector<SamplePointWithWeight> SamplePointList;

//        long count = 0;

        for(int y = 5; y < img.rows - 5; y += 2){

            uchar* data = img.ptr<uchar>(y);

            for(int x = 5; x < img.cols - 5; x +=2){

//                count ++;
//                if(count == 71625){
//
//                    cout << "!!!!!!!!!!" << endl;
//                }

                SamplePointWithWeight samplePointWithWeight;

    //            circle(img, drawPoint, 0, Scalar(255,0,255), -1);

                samplePointWithWeight.yaw = (double(x) + 0.5 ) / double(img.cols) * 180.0 * 2.0 - 180.0;
                samplePointWithWeight.pitch = 180.0 / 2.0 - (double(y) + 0.5 ) / double(img.rows) * 180.0;
                samplePointWithWeight.weight = data[x];

                SamplePointList.push_back(samplePointWithWeight);

    //            imshow("Img", img);
    //            waitKey(30);
            }
        }

        outFile << SamplePointList.size() << endl;
        for(vector<SamplePointWithWeight>::iterator iter = SamplePointList.begin(); iter != SamplePointList.end(); iter ++) {
            outFile << iter->yaw << " " << iter->pitch << " " << iter->weight << endl;
        }

        cout << outputPrefix + to_string(index) + ".txt  finished ..." << endl;
    }

    return 0 ;
}


